package sell;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

/**
 * Created by webrx on 2017/6/14.
 */
@WebServlet("/fuzhuang_xiao.do")    @MultipartConfig
public class Fuzhuang_xiao extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String times =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        DbTest db = new DbTest();
        String name =req.getParameter("name");
        String content =req.getParameter("content");
        String pid =req.getParameter("gccid");
        int pccid =Integer.parseInt(pid);

        String price =req.getParameter("price");
        double priced =Double.parseDouble(price);
        String path =req.getServletContext().getRealPath("upload/fuzhuang");
        File f = new File(path);
        if(!f.exists()){
            f.mkdirs();
        }

        Part part = req.getPart("file");

        String name01 =part.getHeader("content-disposition");

        String fn =name01.substring(name01.lastIndexOf("=\"")+2,name01.length()-1);

        part.write(path+"/mid/"+fn);
        db.small(path+"/mid/"+fn,path+"/big/"+fn,800);
        db.small(path+"/mid/"+fn,path+"/small/"+fn,70);
        db.insert("insert into fuzhuang_xiao values(null,?,?,?,?,?)",new Object[]{name,content,pccid,priced,times});
        db.insert("insert into fuzhang_img values(null,?,?)",new Object[]{fn,pccid});

    }
}
